package com.atguigu.study.interactive;


import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


// V1 synchronized + wait + notifyAll 传统老版本
/*
class AirCondition //资源类（空调） OOP = Field + Method
{
    private int number = 0;// 有一个初始值为零的变量

    public  synchronized void increment() throws InterruptedException
    {

        // 1 判断
        while(number != 0)
        {
            this.wait(); // C  A
        }
        //2 干活
        System.out.println(Thread.currentThread().getName()+"\t"+(++number));
        //3 通知
        this.notifyAll();
    }

    public synchronized void decrement() throws InterruptedException
    {
        //1 判断
        while(number == 0)
        {
            this.wait();
        }
        //2 干活
        System.out.println(Thread.currentThread().getName()+"\t"+(--number));
        //3 通知
        this.notifyAll();
    }
}
*/


//V2 JUC新版，Lock + condition.await() + condition.signalAll()
class AirCondition //资源类（空调） OOP = Field + Method
{
    private int number = 0;// 有一个初始值为零的变量

    final Lock lock = new ReentrantLock();
    final Condition condition  = lock.newCondition();
    public void increment() throws InterruptedException
    {
        lock.lock();
        try
        {
            // 1 判断
            while(number != 0)
            {
                condition.await();
            }
            //2 干活
            System.out.println(Thread.currentThread().getName()+"\t"+(++number));
            //3 通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }
    }

    public void decrement() throws InterruptedException
    {
        lock.lock();
        try
        {
            // 1 判断
            while(number == 0)
            {
                condition.await();
            }
            //2 干活
            System.out.println(Thread.currentThread().getName()+"\t"+(--number));
            //3 通知
            condition.signalAll();
        }finally {
            lock.unlock();
        }

    }
}

/**
 * @auther zzyybs@126.com
 * @Date 2025-04-22 8:19
 * @Description:
 *
 * 有一个初始值为零的变量，现有两个线程对该变量操作，
 * 实现一个线程对变量加1，实现一个线程对变量减1,交替来10轮
 *
 *  *  笔记：Java里面如何进行工程级别的多线程编写
 *  1 多线程变成模板（套路）
 *    高内聚低耦合前提下，线程    操作    资源类
 *
 *  2 多线程交互问题时， 判断    干活    通知
 *
 *  3 需要注意防止虚假唤醒，wait方法需要用到while
 *
 *  4
 */
public class ProdConsumerDemo
{
    public static void main(String[] args)
    {
        AirCondition airCondition = new AirCondition();

        new Thread(() -> {
            try
            {
                for (int i = 0; i < 10; i++) {
                    //暂停毫秒
                    try { TimeUnit.MILLISECONDS.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); }
                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"A").start();

        new Thread(() -> {
            try
            {
                for (int i = 0; i < 10; i++) {
                    try { TimeUnit.MILLISECONDS.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); }

                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"B").start();

        new Thread(() -> {
            try
            {
                for (int i = 0; i < 10; i++) {
                    try { TimeUnit.MILLISECONDS.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); }

                    airCondition.increment();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"C").start();

        new Thread(() -> {
            try
            {
                for (int i = 0; i < 10; i++) {
                    try { TimeUnit.MILLISECONDS.sleep(400); } catch (InterruptedException e) { e.printStackTrace(); }

                    airCondition.decrement();
                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        },"D").start();

    }

}
